#include "light_send_handle.h"

QueueHandle_t spi1_queue_handle;     //spi队列缓存句柄
QueueHandle_t spi2_queue_handle;    //spi2队列缓存句柄

static TaskHandle_t spi1_handle;     //spi任务句柄
static TaskHandle_t spi2_handle;    //spi2任务句柄

spi_device_handle_t spi1;    //spi硬件通道句柄
spi_device_handle_t spi2;   //spi2硬件通道句柄


static u8 data1[3000];
static u8 data2[3000];

static void print_data_hex(uint8_t *data, u16 len)
{   
    for (int i = 0; i < len; i++) 
    {
        printf(" %02x", data[i]);
    }
    printf(", len1: %d\n", len);
}

void set_select_gpio_level(uint8_t i)
{
    switch (i)
    {
    case 0:
        gpio_set_level(PIN_S0, 0);
        gpio_set_level(PIN_S1, 0);
        gpio_set_level(PIN_S2, 0);

        break;

    case 1:
        gpio_set_level(PIN_S0, 1);
        gpio_set_level(PIN_S1, 0);
        gpio_set_level(PIN_S2, 0);

        break;

    case 2:
        gpio_set_level(PIN_S0, 0);
        gpio_set_level(PIN_S1, 1);
        gpio_set_level(PIN_S2, 0);
        break;
    case 3:
        gpio_set_level(PIN_S0, 1);
        gpio_set_level(PIN_S1, 1);
        gpio_set_level(PIN_S2, 0);
        break;
    case 4:
        gpio_set_level(PIN_S0, 0);
        gpio_set_level(PIN_S1, 0);
        gpio_set_level(PIN_S2, 1);
        break;
    case 5:
        gpio_set_level(PIN_S0, 1);
        gpio_set_level(PIN_S1, 0);
        gpio_set_level(PIN_S2, 1);
        break;
    case 6:
        gpio_set_level(PIN_S0, 0);
        gpio_set_level(PIN_S1, 1);
        gpio_set_level(PIN_S2, 1);
        break;
    case 7:
        gpio_set_level(PIN_S0, 1);
        gpio_set_level(PIN_S1, 1);
        gpio_set_level(PIN_S2, 1);
        break;
            
    default:
        break;
    }

}

void init_select_gpio()
{
    device_gpio_init(PIN_S0,GPIO_MODE_OUTPUT,INPUT_PULLNONE);
    device_gpio_init(PIN_S1,GPIO_MODE_OUTPUT,INPUT_PULLNONE);
    device_gpio_init(PIN_S2,GPIO_MODE_OUTPUT,INPUT_PULLNONE);
}






void spi1_task()
{
    LOG_INFO("disp_task : enter spi1_task()\n");
    spi1_queue_handle = xQueueCreate(10,sizeof(spi_send_param *));
    spi_send_param *spi_send_buffer =NULL;
    
    init_select_gpio();
    set_select_gpio_level(1);

    while (1)
    {
        memset(data1,0,3000); //数组清零
        if(xQueueReceive(spi1_queue_handle,&spi_send_buffer,portMAX_DELAY))
        {
            if (spi_send_buffer != NULL)
            {   
                light_transition_send_handle[SPI1_LIGHT_TYPE].handle(data1,spi_send_buffer->send_data,spi_send_buffer->send_len);
                send_data(data1,((spi_send_buffer->send_len)*8),spi1); //按灯组发送
                vPortFree(spi_send_buffer->send_data);
                spi_send_buffer->send_data = NULL;
                vPortFree(spi_send_buffer);
            }
            else
            {
                LOG_ERROR("spi1_task() queue receive err\n");
            }
            
        }
        spi_send_buffer = NULL;
    }
    
}

void spi2_task()
{
    LOG_INFO("disp_task : enter spi2_task()\n");
    spi2_queue_handle = xQueueCreate(10,sizeof(spi_send_param *));
    spi_send_param *spi2_send_buffer = NULL;

    while (1)
    {
        memset(data2,0,2000); //数组清零
        if(xQueueReceive(spi2_queue_handle,&spi2_send_buffer,portMAX_DELAY))
        {   
            if (spi2_send_buffer != NULL)
            {
                light_transition_send_handle[SPI2_LIGHT_TYPE].handle(data2,spi2_send_buffer->send_data,spi2_send_buffer->send_len);
                send_data(data2,((spi2_send_buffer->send_len)*8),spi2); //按灯组发送
                vPortFree(spi2_send_buffer->send_data);
                vPortFree(spi2_send_buffer);
            }
            else
            {
                LOG_ERROR("spi2_task() queue receive err\n");
            }
            spi2_send_buffer =NULL;
        }
    }
}




void device_spi1_init(void)
{
    printf("device_spi1_int():enter\n");
    spi_init(PIN_NUM_MOSI1,HSPI_HOST2,SPI_DMA_CH1,&spi1,light_send_config[SPI1_LIGHT_TYPE].speed);
    xTaskCreate(spi1_task, "spi1_task", SPI_DISPALY_SIZE, NULL, TASK_PROP_DISPLAY, &spi1_handle);
}


void device_spi2_init(void)
{
    printf("device_spi1_int():enter\n");
    spi_init(PIN_NUM_MOSI2,HSPI_HOST3,SPI_DMA_CH2,&spi2,light_send_config[SPI2_LIGHT_TYPE].speed);
    xTaskCreate(spi2_task,"spi2_task",SPI_DISPALY_SIZE,NULL,TASK_PROP_DISPLAY,&spi2_handle);
}

void light_send_handle_fun_init()
{
    if (SPI1_LIGHT_TYPE != LIGHT_TYPE_NONE) //spi1有效激活
    {
        device_spi1_init();
    }

    if (SPI2_LIGHT_TYPE != LIGHT_TYPE_NONE) //spi2有效激活
    { 
        device_spi2_init();
    }
}